package com.treasury.glasses.persistence;

import com.treasury.glasses.Main;
import javax.persistence.EntityTransaction;

/**
 * DataSource for data persistence.
 * @author <a href="mailto:glasses@audienz.eu">Jean-Luc Burot</a>
 */

public class DataSource {
    private static DataSource internal;
    
    private DataSource() {}
    
    public static DataSource getInstance() {
        if (internal == null) {
            internal = new DataSource();
        }
        
        return internal;
    }
    
    public void save(Object object) {
        EntityTransaction tx = null;
        try {
            tx = Main.getEntityManager().getTransaction();
            tx.begin();

            Main.getEntityManager().persist(object);
            
            tx.commit();
        } catch (RuntimeException ex) {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            
            throw ex;
        }
    }
}
